package com.lilicould.service.impl;

import com.lilicould.dao.EmployeeDao;
import com.lilicould.entity.*;
import com.lilicould.service.EmployeeService;
import com.lilicould.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;

public class EmployeeServiceImpl implements EmployeeService {


    @Override
    public Employee getEmployeeById(String id) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
            return employeeDao.selectEmployeeById(id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    @Override
    public ArrayList<Employee> getAllEmployees() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
            return employeeDao.selectAllEmployees();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    // 通过员工id获取员工角色名
    @Override
    public ArrayList<String> getRoleNamesById(String employeeId) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            ArrayList<String> roleNames = new ArrayList<>();
            EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
            // 先通过员工ID获取员工-角色信息
            ArrayList<EmployeeHasRole> employeeHasRoles = employeeDao.selectEmployeeHasRoleByEmployeeId(employeeId);
            for (EmployeeHasRole employeeHasRole : employeeHasRoles) {
                // 通过角色ID获取角色名
                EmployeeRole employeeRole = employeeDao.selectEmployeeRoleById(employeeHasRole.getRoleId());
                roleNames.add(employeeRole.getRoleName());
            }
            return roleNames;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    // 通过员工ID获取技能
    @Override
    public ArrayList<String> getSkillsById(String employeeId) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
            ArrayList<EmployeeHasRole> employeeHasRole = employeeDao.selectEmployeeHasRoleByEmployeeId(employeeId);
            ArrayList<String> skills = new ArrayList<>();
            for (EmployeeHasRole employeeHasRole1 : employeeHasRole) {
                skills.add(employeeHasRole1.getSkill());
            }
            return skills;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    // 通过employeeId获取用户名
    @Override
    public String getUserNameById(String employeeId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
        return employeeDao.selectUsernameByEmployeeId(employeeId);
    }

    // 更新员工信息
    @Override
    public boolean updateEmployee(Employee employee) {
        SqlSession sqlSession = null;
        sqlSession = MyBatisUtil.getSqlSession();
        try {
            EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
            int result = employeeDao.updateEmployee(employee);
            return result > 0;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        } finally {
            sqlSession.close();
        }
    }

    // 获取所有员工详细信息
    @Override
    public ArrayList<EmployeeDetails> getAllEmployeeDetails() {
        ArrayList<EmployeeDetails> employeeDetails = new ArrayList<>();
        AccountServiceImpl accountService = new AccountServiceImpl();
        ArrayList<Employee> employees = this.getAllEmployees();
        ArrayList<String> roleNames;
        ArrayList<String> skills;
        for (Employee employee : employees) {
            String employeeId = employee.getEmployeeId();
            Account account = accountService.selectAccountById(employeeId);
            if (account.getStatus() == 0) // 账号被禁用时跳过
                continue;
            roleNames = this.getRoleNamesById(employeeId);
            skills = this.getSkillsById(employeeId);
            EmployeeDetails employeeDetail = new EmployeeDetails(employee, roleNames, skills);
            employeeDetails.add(employeeDetail);
        }
        return employeeDetails;
    }
}
